<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>

    <script src="./libs/https.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            用户信息修改
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="form">
                <div class="form-group">
                    <label for="inputEmail1" class="col-sm-2 control-label">用户名称：</label>
                    <div class="col-sm-4">
                        <input type="text" name="username" class="form-control username" id="inputEmail1" value="李思思">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail2" class="col-sm-2 control-label">昵称：</label>
                    <div class="col-sm-4">
                        <input type="text" name="nickname" class="form-control nickname" id="inputEmail2" value="哈哈思">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">邮箱：</label>
                    <div class="col-sm-4">
                        <input type="text" name="email" class="form-control email" id="inputEmail3" value="520@163.com">
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">用户图标：</label>
                    <div class="col-sm-10">
                        <img src="images/2.jpg" alt="" class="user_pic">
                        <input style="display: block" name="userPic" type="file" id="exampleInputFile">
                        <!-- 给label标签的for属性赋值,值是input标签的id,那这样的话点击label就相当于点击了input -->
                        <br>
                        <label for="exampleInputFile">
                            <img src="./images/uploads_icon.jpg" style="height: 50px;" alt="">
                        </label>
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputEmail4" class="col-sm-2 control-label">密码：</label>
                    <div class="col-sm-4">
                        <input type="password" name="password" class="form-control password" id="inputEmail4"
                            value="12345678">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-success btn-edit">修改</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</body>

</html>

<script>
    $(function () {
        //1.发起ajax请求，获得用户详细信息
        $.ajax({
            type: 'get',
            url: BigNew.user_detail,
            success: function (backData) {
                // console.log(backData);
                if (backData.code == 200) {
                    //2.将返回的数据渲染到指定结构

                    for (let key in backData.data) {
                        $('.' + key).val(backData.data[key])
                        // console.log(backData.data[key]);
                    }
                    //单独设置头像
                    $('.user_pic').attr('src', backData.data.userPic)
                }
            }
        })
        //3.头像图片预览功能
        //利用onchange方法，监听上传按钮
        //获得图片文件，将文件变成路径，再将路径赋值给img
        $('#exampleInputFile').on('change', function () {
            let PicFile = this.files[0]
            let picUrl = URL.createObjectURL(PicFile)
            $('.user_pic').attr('src', picUrl)
        })
        //4.设置修改按钮的点击事件
        $('.btn-edit').on('click', function (e) {
            e.preventDefault()
            //5.利用formData获得所有数据
            //前提:所有的表单控件name值必须与参数一致
            let fd = new FormData(document.querySelector('form'))
            // window.location='./index.html'
            //6.发起ajax请求，修改用户数据
            $.ajax({
                type: 'post',
                url: BigNew.user_edit,
                data: fd,
                contentType: false,
                processData: false,
                success: function (backData) {
                    if (backData.code == 200) {
                        //7.当数据修改成功，应该刷新页面
                        //index页面，可是现在代码在user页面中
                        //在子页面中如何访问父页面：parent==index页面
                        //前提：使用live插件打开页面才可以使用parent
                        //如果没有live插件打开项目，没用将有项目的环境变成服务器环境
                        //正常浏览器中，是无法运行跨页面访问数据的
                        parent.window.location.reload()
                    }
                }
            })
        })
    })
</script>